<template>
  <el-card>
    <div slot="header" class="clearfix">
      <span>员工业绩</span>
    </div>
    <div class="clearfix mb15">
    <v-searchForm
      :form-data="filter_info"
      :form-title="formTitle"
      @search="search"
      @reset="reset"/>
    </div>

    <v-table
      :table-title="tableTitle"
      :table-data="tableData"
      :default-operate="false"
      :select="false"
      btn-class="btn"
      @sortChange="sortChange"
    />
    <v-pagination
    :pageInfo="pageInfo"
    :getTableList="getTableList"
    />
  </el-card>
</template>

<script>
  import { parseTime } from '../../../utils'
export default {
  name: 'Index',
  data() {

    console.log(''|0)
    const endTime = parseTime(new Date(), '{y}-{m}-{d}')
    const start = new Date()
    start.setTime(start.getTime() - 3600 * 1000 * 24 * (start.getDate() - 1));
    const startTime = parseTime(start, '{y}-{m}-{d}');

    console.log(new Date(2019,start.getMonth()));
    return {
      // f分页
      pageInfo: {
        pageNumber: 1,
        pageSize: 30,
        totalPage: 0,
        totalRow: 0
      },

      sidx: 'id',
      sortKey: null,
      exportTitle:[],
      exportData:[],
      filter_info: {
        created_at: [startTime, endTime]
      },
      originFilter: {
        created_at: [startTime, endTime]
      },
      taompoperaterinfo: [],
      tableData: []

    }
  },
  computed: {
    tableTitle() {
      return ([
        {
          cn: 'id',
          en: 'id',
          sortable: true
        },
        {
          cn: '人员名称',
          en: 'name',
        },
        {
          cn: '预估收益',
          en: 'income',
          sortable: true
        },
        {
          cn: '实际收益',
          en: 'act_income',
          sortable: true
        },
        {
          cn: '最后一日单粉产出',
          en: 'avg_fan_income',
          sortable: true
        },
      ])
    },
    filter_infos() {
      const data = {}
      const origin = this.filter_info
      for (const key in origin) {
        if (!this.filter_info[key] && this.filter_info[key] !== 0) {
          this.$delete(this.filter_info, key)
          continue
        }
        if (key === 'created_at') {
          data.begin_date = origin[key][0]
          data.end_date = origin[key][1]
        } else {
          data[key] = origin[key]
        }
      }
      if (!origin.created_at) {
        delete data.start_time
        delete data.End_time
      }
      return data
    },
    formTitle(){
      return([
        {
          cn: '人员名称：',
          en: 'staff_name',
          // type:'select',
          // list:this.taompoperaterinfo
        },
        {
          cn: '时间：',
          en: 'created_at',
          type: 'time'
        }
      ])
    },
  },
  activated() {
    this.getTableList()
    // this.getFormValue()
  },
  methods: {
    getTableList() {
      const datas = {
        ...this.filter_infos,
        sidx: this.sidx,
        sortKey: this.sortKey,
        page: this.pageInfo.pageNumber, rows: this.pageInfo.pageSize
      }
      this.$get('action/TaoMpOperaterInfo/dataGrid', datas)
        .then(res => {
          const list1 = res.data.list
          this.tableData = list1.map(item => {
            const obj = item
            obj.ratio =item.ratio||0
            obj.avg_fan_income =item.avg_fan_income&&item.avg_fan_income.toFixed(4)||0
            return obj
          })
          this.pageInfo.totalPage = res.data.totalPage;
          this.pageInfo.totalRow = res.data.totalRow;
        })
    },
    // 获取员工列表
    getFormValue() {
      this.$get('action/TaoMpInfo/formValue')
        .then(res => {
          const taompoperaterinfo = []
          res.data.taompoperaterinfo.forEach(item => {
            taompoperaterinfo.push({
              label: item.name,
              value: item.name
            })
          })
          this.taompoperaterinfo = this.taompoperaterinfo.concat(taompoperaterinfo)
        })
        .catch(_ => console.log(1))
    },

    // 搜索
    search() {
      this.pageInfo.pageNumber=1
      this.getTableList()
    },

    reset() {
      this.filter_info = {}
      this.getTableList()
    },

    sortChange({ column, prop, order }) {
      this.sidx = prop || 'id'
      this.sortKey = order === 'ascending' ? 'asc' : null
      this.getTableList()
    }
  }
}
</script>

<style scoped>

</style>
